/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.carpark.dao;

import cz.muni.fi.pa165.carpark.entities.BusinessTrip;
import cz.muni.fi.pa165.carpark.entities.Employee;
import cz.muni.fi.pa165.carpark.entities.Vehicle;
import java.util.List;
import javax.persistence.*;
import org.springframework.stereotype.Repository;

/**
 * 
 * BusinessTripDAO implementation
 * 
 * @author Ado
 */
@Repository
public class BusinessTripDAOImpl implements BusinessTripDAO {
    
    @PersistenceContext
    private  EntityManager em;    
    
    
    public void create(BusinessTrip businessTrip){
        if(businessTrip == null){
            throw new IllegalArgumentException("BusinessTrip cannot be null.");
        }
        if(businessTrip.getId() != null){
            throw new IllegalArgumentException("BusinessTrip already has an id(it is already in the database).");
        }
        try{
            em.persist(businessTrip);
        }catch(EntityExistsException ex){
            throw new IllegalArgumentException("BusinessTrip is in DB");
        }
    }

    public BusinessTrip read(Long id){
        if(id == null){
            throw new IllegalArgumentException("Id cannot be null.");
        }
        if(id <=0 ){
            throw new IllegalArgumentException("Id muss be positive.");
        }
        BusinessTrip businessTrip = em.find(BusinessTrip.class, id);
        return businessTrip;
    }

    public void update(BusinessTrip businessTrip){
        if(businessTrip == null){
            throw new IllegalArgumentException("BusinessTrip cannot be null.");
        }
        if(businessTrip.getId() == null || businessTrip.getId()<=0){
            throw new IllegalArgumentException("BusinessTrip id cannot be null or negative.");
        }
        try{
            em.merge(businessTrip);
        }catch(EntityNotFoundException ex){
            throw new IllegalArgumentException();
        }
    }

    public void delete(BusinessTrip businessTrip){
        if(businessTrip == null){
            throw new IllegalArgumentException("BusinessTrip cannot be null.");
        }
        if(businessTrip.getId() == null || businessTrip.getId()<=0){
            throw new IllegalArgumentException("BusinessTrip id cannot be null or negative.");
        }
        BusinessTrip inDB = em.find(BusinessTrip.class, businessTrip.getId());
        if (inDB != null) {
            em.remove(inDB);
        }else{
            throw new IllegalArgumentException("BusinessTrip isn't in db.");
        }
    }

    public List<BusinessTrip> getEmployeeBusinessTrips(Employee employee){
        if (employee == null) {
            throw new IllegalArgumentException("Employee cannot be null.");
        }
        if (employee.getId() == null) {
            throw new IllegalArgumentException("Employee already does not have an id. It has not been stored in the databse yet.");
        }
        TypedQuery<BusinessTrip> query = em.createQuery("SELECT b FROM BusinessTrip b WHERE b.employee = :employee", BusinessTrip.class);
        query.setParameter("employee", employee);
        return query.getResultList();
    }

    public List<BusinessTrip> getVehiclesBusinessTrips(Vehicle vehicle){
        if (vehicle == null) {
            throw new IllegalArgumentException("vehicle cannot be null.");
        }
        if (vehicle.getId() == null) {
            throw new IllegalArgumentException("vehicle already does not have an id. It has not been stored in the databse yet.");
        }
        TypedQuery<BusinessTrip> query = em.createQuery("SELECT b FROM BusinessTrip b WHERE b.vehicle.id = :vehicle", BusinessTrip.class);
        query.setParameter("vehicle", vehicle.getId());
        return query.getResultList();
    }

    public List<BusinessTrip> findAll() {
        TypedQuery<BusinessTrip> query = em.createQuery("SELECT b FROM BusinessTrip b", BusinessTrip.class);
        return query.getResultList();
    }
    
    public void setEntityManagerFactory(EntityManager em) {
        this.em = em;
    }
}
